CXX = g++
CXXFLAGS += -g -std=c++14 -O3
CXXSFLAGS += -shared -fPIC

LIB_DIR = ../lib
BIN_DIR = ../bin
INC_DIR = ../include

INC  += -I/usr/local/include -I/usr/include -I$(INC_DIR) -I/usr/include/mysql \

LIBS = -L/lib64 -lfastcgipp -lboost_thread -lboost_system -lboost_system -lboost_date_time -lpthread \
		
COMM_LIB = -L$(LIB_DIR) -lcomm -L/lib64 -lfastcgipp -lpthread \
			-lboost_thread -lboost_system -lboost_system -lboost_date_time \
			-L/lib64 -lhiredis \
			-L/lib64 -lspnetkit \
			-L/lib64/mysql -lmysqlclient \

TARGETS = $(LIB_DIR)/libcomm.so	\
		$(BIN_DIR)/test.fcgi	\

COMM_DIR = comm
CONTROLLER_DIR = controller
MODEL_DIR = model
BEAN_DIR = bean

SRC_COMM = $(wildcard $(COMM_DIR)/*.cpp)
SRC_CONTROLLER = $(wildcard $(CONTROLLER_DIR)/*.cpp)
SRC_MODEL = $(wildcard $(MODEL_DIR)/*.cpp)
SRC_BEAN = $(wildcard $(BEAN_DIR)/*.cpp)

OBJ_COMM = $(patsubst %.cpp,%.o,$(SRC_COMM))
OBJ_CONTROLLER = $(patsubst %.cpp,%.o,$(SRC_CONTROLLER))
OBJ_MODEL = $(patsubst %.cpp,%.o,$(SRC_MODEL))
OBJ_BEAN = $(patsubst %.cpp,%.o,$(SRC_BEAN))

#_COMM = $(notdir $(SRC_COMM))
#_CONTROLLER = $(notdir $(SRC_CONTROLLER))
#_BEAN = $(notdir $(SRC_BEAN))

#OBJ_COMM = $(patsubst %.cpp,%.o,$(_COMM))
#OBJ_CONTROLLER = $(patsubst %.cpp,%.o,$(_CONTROLLER))
#OBJ_BEAN = $(patsubst %.cpp,%.o,$(_BEAN))

CLEANUP = rm -rf $(TARGETS) $(OBJ_COMM) $(OBJ_CONTROLLER) $(OBJ_MODEL)

all:$(TARGETS)
	
clean:
	$(CLEANUP)

#$(LIB_DIR)/libcomm.so:$(SRC_COMM) $(SRC_CONTROLLER)
#	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) $^ -o $@

$(LIB_DIR)/libcomm.so:$(OBJ_COMM) $(OBJ_CONTROLLER) $(OBJ_MODEL)
	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) $^ -o $@
	ln -s -f /root/cpp/fastcgippweb/lib/libcomm.so /lib64/libcomm.so

$(BIN_DIR)/test.fcgi:main.o
	$(CXX) $(CXXFLAGS) $(INC) $(COMM_LIB)  $^ -o $@

#$(COMM_DIR)/$(OBJ_COMM):$(SRC_COMM)
#	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) $(SRC_COMM) -c

#$(CONTROLLER_DIR)/$(OBJ_CONTROLLER):$(SRC_CONTROLLER)
#	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) $(SRC_CONTROLLER) -c

%.o:%.cpp
	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) -c $< -o $@

$(COMM_DIR)/%.o: $(COMM_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) -c $< -o $@

$(CONTROLLER_DIR)/%.o:$(CONTROLLER_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) -c $< -o $@
	
$(MODEL_DIR)/%.o:$(MODEL_DIR)/%.cpp
	$(CXX) $(CXXFLAGS) $(CXXSFLAGS) $(INC) -c $< -o $@
	
